* Aggregate analyses

***** Risk

cd "$pathdata/Risk"
use Risk_cleaned, clear

keep if baseline_set == 1
keep if lottery_amount > 0

replace ce=ce/100
replace lottery_probability = lottery_probability/100

gen delta=.
gen gamma=.
gen beta=.
gen default=.
gen alpha=.

// * Restricted Model
nl (switching_point= lottery_probability^(1/{alpha=1})*lottery_amount), cl(id)  
predict pred_restricted
estat ic
replace pred_restricted = pred_restricted/lottery_amount


// * With CU
nl (switching_point = (1-min(1,max(0,(1-{gamma=1}*cognitive_uncertainty)))) * {default=1}*lottery_amount + min(1,max(0,(1-{gamma=1}*cognitive_uncertainty))) * lottery_probability^(1/{alpha=1})*lottery_amount), cl(id)
predict pred_cu
estat ic
predict res, residuals
replace pred_cu = (pred_cu/lottery_amount)


***** PLOTS *****

preserve

foreach i in pred_restricted pred_cu lottery_probability ce {
	replace `i'=`i'*100
}

collapse (median) ce cognitive_uncertainty pred_cu pred_restricted (semean) se_ce=ce se_cu=pred_cu se_rest = pred_restricted (count) no = ce, by(lottery_probability)

gen upper=ce+se_ce
gen lower=ce-se_ce
gen upper_cu=pred_cu+se_cu
gen lower_cu=pred_cu-se_cu
gen upper_res=pred_restricted+se_rest
gen lower_res=pred_restricted-se_rest



tw (scatter ce lottery_probability, msize(large)  ms(+) mcolor(black)) ///
	(line pred_cu lottery_probability, lwidth(medthick) lcolor(red%80))  ///
 	(function y=x , range(0 100) lpattern(dash) lcolor(black) lwidth(thin) ), ///
	ytitle("Normalized certainty equivalent") ///
    xtitle("Payoff probability") xscale( lcolor(none)) ysc(lcolor(none)) ///
	yline(0, lcolor(gs10) lwidth(thin)) ///
	graphregion(color(white) margin(l=5 r=5 t=0 b=0)) ///
	ylabel(, tlc(none) angle(0) glcolor(gs15) glwidth(thin)) ///
	legend(order(1 2) label(1 "Subject Data") label(2 "CU Model") r(2))

restore
